package com.xiaoniu.lib_keleim.utils;

import android.util.Log;

import com.xiaoniu.lib_keleim.pojo.vos.AckMessage;

import java.lang.reflect.Field;

/**
 * 自定义数据流消息协议
 * ~> 包起始位置   126 62
 *
 * <~ 包结束位置  60 126
 *
 */
public class ProtocolUtil {

    public byte[] toBytes(AckMessage message) {
        StringBuilder data = new StringBuilder();
        Field[] declaredFields = message.getClass().getDeclaredFields();
        for (int i = 0; i < declaredFields.length; i++) {
            Field field = declaredFields[i];
            ProtocolField annotation = field.getAnnotation(ProtocolField.class);
            if (annotation!=null) {
                try {
                    data.append(String.format(" @%s %s", annotation.index(),field.get(message)));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }

        data.insert(0,">");
        data.insert(0,"~");

        data.append("<");
        data.append("~");
        Log.d("conversations > " , " " +data);
        return data.toString().getBytes();
    }
}
